<template>
  <header class="app-header">
    <!-- 引入 Font Awesome 图标库 -->
    <link
      href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css"
      rel="stylesheet"
    >

    <div class="header-content">
      <div class="container">
        <!-- 左侧图标与标题 -->
        <div class="flex items-center gap-2">
          <i class="fa fa-graduation-cap text-primary text-xl" />
          <h1 class="text-xl font-bold text-gray-800">
            学习平台
          </h1>
        </div>

        <!-- 导航栏 -->
        <nav class="hidden md:flex items-center space-x-8">
          <router-link
            to="/mainPage"
            class="nav-link px-4"
          >
            首页
          </router-link>
        </nav>

        <!-- 用户操作 -->
        <div class="flex items-center space-x-4">
          <div class="dropdown">
            <button class="dropdown-toggle flex items-center gap-2 px-3 py-2 rounded-full hover:bg-gray-100 transition-all border border-transparent hover:border-gray-200">
              <img
                src="https://picsum.photos/40/40?random=user"
                alt="用户头像"
                class="h-8 w-8 rounded-full object-cover"
              >
              <span class="font-medium text-gray-700 hidden sm:inline-block">{{ user?.userName || '未登录用户' }}</span>
            </button>

            <!-- 下拉菜单 -->
            <div class="dropdown-menu">
              <router-link
                class="dropdown-item flex items-center gap-2 px-4 py-2 hover:bg-gray-50"
                to="/userCenter"
              >
                <i class="fa fa-user-circle text-gray-500 w-5" />
                <span>个人中心</span>
              </router-link>
              <a
                class="dropdown-item flex items-center gap-2 px-4 py-2 hover:bg-gray-50"
                href="#"
              >
                <i class="fa fa-cog text-gray-500 w-5" />
                <span>设置</span>
              </a>
              <div class="dropdown-divider my-1" />
              <a
                class="dropdown-item flex items-center gap-2 px-4 py-2 hover:bg-gray-50 text-red-500"
                href="#"
                @click.prevent="logout"
              >
                <i class="fa fa-sign-out text-red-500 w-5" />
                <span>退出登录</span>
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </header>
</template>

<script>
import { useRouter } from 'vue-router';
import { useUserStore } from '@/stores/userStore';

export default {
  name: 'AppHeader',
  props: {
    user: {
      type: Object,
      default: () => ({
        userName: null
      })
    }
  },
  setup(props, { emit }) {
    const router = useRouter();
    const userStore = useUserStore();

    function logout() {
      // 清除 Pinia 中的用户状态
      userStore.$patch({
        user: null,
        isAuthenticated: false
      });

      // 跳转到登录页
      router.push('/login');
    }

    return {
      logout
    };
  }
};
</script>

<style scoped>
/* 保留原来的基础样式 */
.app-header {
  background-color: #ffffff;
  border-bottom: 1px solid #e9ecef;
  box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
}

.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

.container > div:first-child {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.container h1 {
  margin: 0;
  font-size: 1.2rem;
}

.container > div:last-child {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.container img {
  border-radius: 50%;
}

.nav-link {
  font-size: 20px;
  position: relative;
  display: inline-block;
  padding: 0.5rem 0;
  color: #495057;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s ease;
}

.nav-link::after {
  content: '';
  position: absolute;
  width: 0;
  height: 2px;
  bottom: 0;
  left: 0;
  background-color: #3b82f6;
  transition: width 0.3s ease;
}

.nav-link:hover {
  color: #3b82f6;
}

.nav-link:hover::after {
  width: 100%;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: none;
  border: 1px solid transparent;
  color: #000;
  padding: 0.375rem 0.75rem;
  border-radius: 0.25rem;
  cursor: pointer;
  font-size: 1rem;
  transition: all 0.2s ease;
}

.dropdown-toggle:hover {
  background-color: #e9ecef;
}

.dropdown-toggle i {
  color: #000;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: 100%;
  right: 0;
  min-width: 160px;
  padding: 0.5rem 0;
  margin: 0.125rem 0 0;
  font-size: 1rem;
  color: #212529;
  background-color: #fff;
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-radius: 0.25rem;
  box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.175);
  z-index: 1000;
  transform-origin: top right;
  transform: scale(0.95);
  opacity: 0;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.dropdown:hover .dropdown-menu {
  display: block;
  transform: scale(1);
  opacity: 1;
}

.dropdown-item {
  display: block;
  width: 100%;
  padding: 0.25rem 1.5rem;
  clear: both;
  font-weight: 400;
  color: #212529;
  text-align: inherit;
  text-decoration: none;
  white-space: nowrap;
  background-color: transparent;
  border: 0;
  transition: background-color 0.2s ease;
}

.dropdown-item:hover {
  color: #16181b;
  background-color: #f8f9fa;
}

.dropdown-divider {
  height: 0;
  margin: 0.5rem 0;
  overflow: hidden;
  border-top: 1px solid #e9ecef;
}
</style>